<template>
  <page-dialog
    :title="title"
    v-model:visible="shows"
    :width="dialogInfo.width"
    :bt-list="dialogInfo.btList"
  >
    <!-- 条件栏 -->
    <new-page-filter
      v-model:query="filterInfo.query.queryMap"
      :query-more-json="true"
      :filter-list="filterInfo.searchList"
      :filter-btn="filterInfo.searchBtn"
    >
    </new-page-filter>
    <el-form ref="tableForm" :model="tableInfo" style="width: 100%">
      <dialog-table
        ref="resourceTable"
        :refresh="tableInfo.refresh"
        :init-curpage="tableInfo.initCurpage"
        v-model:data="tableInfo.data"
        :api="tableInfo.api"
        v-model:query="filterInfo.query"
        :field-list="tableInfo.fieldList"
        :handle="tableInfo.handle"
        :rowKey="rowKey"
        :highlight-row="highlightRow"
        :checkedRowList="checkedRowList"
        :reserve-selection="true"
        :check-box="checkBox"
        @handleEvent="handleEvent"
      >
        <template v-slot:col-editQty="scope">
          <el-form-item class="margin-bottom" @click.stop.native="">
            <div class="page-table-input-number-wrapper">
              <div class="page-table-input-number-left">
                <div class="page-table-input-number-item">
                  <el-form-item>
                    <el-input-number
                      v-model.lazy="scope.row.editQty"
                      style="width: 100%; margintop: 18px"
                      clearable
                      controls-position="right"
                      :precision="systemConfig.qty_show_precision"
                      :min="0"
                      :max="99999999"
                    />
                  </el-form-item>
                  <div class="page-table-input-number-item-unite">
                    {{ scope.row.networkUnit }}
                  </div>
                </div>
              </div>
            </div>
          </el-form-item>
        </template>
        <template v-slot:col-provinceCalcQty="scope">
          <el-form-item class="margin-bottom" @click.stop.native="">
            <div class="page-table-input-number-wrapper">
              <div class="page-table-input-number-left">
                <div class="page-table-input-number-item">
                  <el-form-item>
                    <el-input-number
                      v-model.lazy="scope.row.provinceCalcQty"
                      style="width: 100%; margintop: 18px"
                      clearable
                      controls-position="right"
                      :precision="systemConfig.qty_show_precision"
                      :min="0"
                      :max="largeMax(scope.row)"
                    />
                  </el-form-item>
                  <div class="page-table-input-number-item-unite">
                    {{ scope.row.networkUnit }}
                  </div>
                </div>
              </div>
            </div>
          </el-form-item>
        </template>
        <template v-slot:col-networkUnitScale="scope">
          {{
            scope.row.systemUnitCoefficient +
            scope.row.unitName +
            '=' +
            scope.row.networkUnitCoefficient +
            scope.row.networkUnit
          }}
        </template>
        <template v-slot:col-provinceAvailableQty="scope">
          <span
            v-if="
              scope.row.provinceAvailableQty ||
              scope.row.provinceAvailableQty === 0
            "
          >
            {{ scope.row.provinceAvailableQty + scope.row.networkUnit }}
          </span>
          <span v-else>-</span>
        </template>
        <template v-slot:col-qty="scope">
          <span v-if="scope.row.qty || scope.row.qty === 0">
            {{ scope.row.qty + scope.row.unitName }}
          </span>
          <span v-else>-</span>
        </template>
        <template v-slot:col-preProvinceCalcQty="scope">
          <span
            v-if="
              scope.row.preProvinceCalcQty || scope.row.preProvinceCalcQty === 0
            "
          >
            {{ scope.row.preProvinceCalcQty + scope.row.unitName }}
          </span>
          <span v-else>-</span>
        </template>
        <template v-slot:col-sysQty="scope">
          <span v-if="scope.row.sysQty || scope.row.sysQty === 0">
            {{ scope.row.sysQty + scope.row.unitName }}
          </span>
          <span v-else>-</span>
        </template>
        <template v-slot:col-price="scope">
          <span v-if="scope.row.price || scope.row.price === 0">
            {{ scope.row.price + '元/' + scope.row.unitName }}
          </span>
          <span v-else>-</span>
        </template>
        <template v-slot:col-sysPrice="scope">
          <span v-if="scope.row.sysPrice || scope.row.sysPrice === 0">
            {{ scope.row.sysPrice + '元/' + scope.row.networkUnit }}
          </span>
          <span v-else>-</span>
        </template>
        <template v-slot:col-provincePrice="scope">
          <span v-if="scope.row.provincePrice || scope.row.provincePrice === 0">
            {{ scope.row.provincePrice + '元/' + scope.row.networkUnit }}
          </span>
          <span v-else>-</span>
        </template>
        <template v-slot:col-provinceSumAmount="scope">
          <span
            v-if="
              scope.row.provinceSumAmount || scope.row.provinceSumAmount === 0
            "
          >
            {{ scope.row.provinceSumAmount + '元' }}
          </span>
          <span v-else>-</span>
        </template>
        <template v-slot:col-sumAmount="scope">
          <span v-if="scope.row.sumAmount || scope.row.sumAmount === 0">
            {{ scope.row.sumAmount + '元' }}
          </span>
          <span v-else>-</span>
        </template>
      </dialog-table>
    </el-form>
  </page-dialog>
</template>
<script setup>
// import PageDialog from '../PageDialog/index'
// import DialogTable from '../DialogTable/index'
// import PageFilter from '../NewPageFilter/index'
// import SlipSelect from '../SlipSelect/index'
import { getCustomizeSetting } from '@/api/customize'
import { getProductCustomizeList, getSupplierAll } from '@/api/core.js'

const { proxy } = getCurrentInstance()
const emits = defineEmits()
const props = defineProps({
  highlightRow: {
    type: Boolean,
    default: false,
  },
  checkBox: {
    type: Boolean,
    default: true,
  },
  rowKey: {
    type: String,
    default: 'id',
  },
  api: {
    type: Function,
    default: getProductCustomizeList,
  },
  showDialog: {
    type: Boolean,
    default: false,
  },
  showPrice: {
    type: Boolean,
  },
  showSupplierName: {
    type: Boolean,
    default: false,
  },
  query: {
    type: Object,
    default: function () {
      return {}
    },
  },
  queryParams: {
    type: Object,
    default: function () {
      return {}
    },
  },
  showStock: {
    type: Boolean,
    default: false,
  },
  tableKey: {
    type: String,
    default:
      'productPageWithDefaultSupplier-com.slip.spd.entity.vo.core.CoreProductVONew',
  },
  checkedList: {
    type: Array,
    default: () => {
      return []
    },
  },
  checkedRowList: {
    type: Array,
    default: () => {
      return []
    },
  },
  saveEmpty: {
    type: Boolean,
    default: false,
  },
  title: {
    type: String,
    default: '选择产品',
  },
  defaultQty: {
    //给数量赋值默认值
    type: Boolean,
    default: false,
  },
})

const systemConfig = ref(proxy.$store.state.permission.systemConfig)

// 过滤相关配置
const filterInfo = ref({
  query: {
    page: true,
    customizeUserVO: {},
    sortString: proxy.sortString,
    queryMap: {
      // type: {
      //   asTbName: 'core_product_new',
      //   columnName: 'type',
      //   queryResource: 0,
      //   queryType: 'EQUALS',
      //   queryCategory: 'AND',
      //   value: 0,
      // },
    },
    queryParams: {},
  },
  searchList: [],
  searchBtn: [{ type: 'button', label: '搜索', event: search }],
})
// 表格相关
const tableInfo = ref({
  refresh: 1,
  initCurpage: 1,
  data: [],
  api: undefined,
  fieldList: [],
})
// 弹窗相关
const dialogInfo = ref({
  width: '90%',
  btLoading: false,
  btList: [
    { label: '关闭', event: handleClose, show: true },
    {
      label: '保存',
      type: 'primary',
      disabled: !props.saveEmpty,
      event: save,
      show: true,
    },
  ],
})
const selectRow = ref([])

const shows = computed({
  get() {
    return props.showDialog
  },
  set(newValue) {
    emits('update:showDialog', newValue) // 更新父组件shows
  },
})

watch(
  () => tableInfo.value.data,
  () => {
    tableInfo.value.data.forEach((row) => {
      let qty = 0
      if (props.defaultQty) {
        qty = row.provinceCalcQty
      } else {
        qty = row.editQty
      }
      row.provinceSumAmount = proxy.$fn.accMul(qty, parseFloat(row.price))
      row.sumAmount = proxy.$fn.accMul(qty, parseFloat(row.price))
    })
  },
  {
    deep: true,
  }
)

setData()
function setData() {
  let obj = {
    orderNo: {
      label: '单号',
      value: 'orderNo',
      hidden: true,
      type: 'column',
      show: false,
      componentType: 'input',
    },
    price: {
      label: '单价',
      value: 'price',
      hidden: true,
      type: 'slot',
      show: false,
      permission: !props.showPrice,
      formatter: proxy.$fn.TripartiteMethod,
    },
    defaultSupplierPrice: {
      label: '默认供应商价格',
      value: 'defaultSupplierPrice',
      type: 'column',
      permission: !props.showPrice,
      show: false,
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    showPrice: {
      label: '展示价格',
      value: 'showPrice',
      type: 'column',
      show: false,
      permission: !props.showPrice,
      formatter: proxy.$fn.TripartiteMethod,
      hidden: true,
    },
    commonName: {
      label: '通用名称',
      value: 'commonName',
      componentType: 'input',
      show: false,
      hidden: true,
    },
    editQty: {
      label: '数量',
      value: 'editQty',
      type: 'slot',
      componentType: 'number',
      show: false,
      hidden: true,
      minWidth: '250px',
    },
    qty: {
      label: '数量',
      value: 'qty',
      type: 'slot',
      hidden: true,
    },
    provinceCalcQty: {
      label: '数量',
      value: 'provinceCalcQty',
      type: 'slot',
      componentType: 'number',
      show: false,
      hidden: true,
      minWidth: '250px',
    },
    sysPrice: {
      label: '系统单价',
      value: 'sysPrice',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !props.showPrice,
      hidden: true,
    },
    provinceSumAmount: {
      label: '省采金额',
      value: 'provinceSumAmount',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !props.showPrice,
      hidden: true,
    },
    sumAmount: {
      label: '总金额',
      value: 'sumAmount',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !props.showPrice,
      hidden: true,
    },
    provincePrice: {
      label: '省采单价',
      value: 'provincePrice',
      type: 'slot',
      formatter: proxy.$fn.TripartiteMethod,
      permission: !props.showPrice,
      hidden: true,
    },
    networkUnitScale: {
      label: 'SPD单位/省采单位',
      value: 'networkUnitScale',
      componentType: 'input',
      type: 'slot',
      hidden: true,
    },
    sysQty: {
      label: '系统采购数量',
      value: 'sysQty',
      type: 'slot',
      hidden: true,
    },
    provinceAvailableQty: {
      label: '省采可采购数量',
      value: 'provinceAvailableQty',
      type: 'slot',
      hidden: true,
    },
    preProvinceCalcQty: {
      label: '省采预采购数量',
      value: 'preProvinceCalcQty',
      type: 'slot',
      hidden: true,
    },
  }
  tableInfo.value.fieldList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  filterInfo.value.searchList = Object.values({
    ...proxy.$fn.deepClone(proxy.$fieldfList.FieldList),
    ...proxy.$fn.deepClone(obj),
  })
  getCustomize()
}

// 获取列表
function getList() {
  tableInfo.value.refresh = Math.random()
}
function search() {
  tableInfo.value.initCurpage = Math.random()
  tableInfo.value.refresh = Math.random()
}
function largeMax(row) {
  return row.provinceAvailableQty
}
function save() {
  handleClose()
  emits('callback', selectRow.value)
}
function handleEvent(event, data) {
  switch (event) {
    case 'list':
      if (props.defaultQty) {
        tableInfo.value.data.forEach((item) => {
          item.provinceCalcQty = item.provinceAvailableQty || 0
        })
      }

      break
    case 'tableClickRow':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
    case 'tableCheck':
      selectRow.value = data
      if (!props.saveEmpty) {
        dialogInfo.value.btList[1].disabled = !selectRow.value
      }
      break
  }
}
// 关闭弹窗前的回调
function handleClose() {
  shows.value = false
  emits('reset')
}
function getCustomize() {
  let param = { tableKey: props.tableKey }
  getCustomizeSetting(param).then((res) => {
    if (res && res.success) {
      let data = res.data
      tableInfo.value.fieldList = proxy.$fn.setFieldList(
        tableInfo.value.fieldList,
        data.detail
      )

      let searchInfo = proxy.$fn.setSearchList(
        filterInfo.value.searchList,
        data.detail
      )

      filterInfo.value.searchList = searchInfo.searchList
      filterInfo.value.searchList.forEach((item) => {
        item.event = search
      })

      filterInfo.value.query.customizeUserVO = data
      filterInfo.value.query.queryMap = {
        ...searchInfo.queryMap,
        ...props.query,
        ...filterInfo.value.query.queryMap,
      }
      filterInfo.value.query.queryParams = {
        ...filterInfo.value.query.queryParams,
        ...props.queryParams,
      }
      tableInfo.value.api = props.api
      if (filterInfo.value.query.queryMap.networkProductType.value == 1) {
        tableInfo.value.fieldList.forEach((item) => {
          if (item.value === 'procureCatalogId') {
            item.hidden = true
          }
        })
      } else {
        tableInfo.value.fieldList.forEach((item) => {
          if (item.value === 'procureCatalogId') {
            item.hidden = false
          }
        })
      }
      getList()
    }
  })
}
</script>

<style scoped lang="scss"></style>
